package com.myfoon.view.context.helper;

import javax.ejb.EJBException;

import javax.persistence.EntityExistsException;

import org.hibernate.exception.ConstraintViolationException;


public class ExceptionHelper {
    public ExceptionHelper() {
    }

    public static int findKeyNo(EJBException ex) {
        int keyNo = 0;
        int pos;
        
        if (ex.getCausedByException() instanceof EntityExistsException) {
            if (ex.getCausedByException().getCause() instanceof
                ConstraintViolationException) {

                ConstraintViolationException cvEx =
                    (ConstraintViolationException)ex.getCausedByException().getCause();
                String line;
                
                for (int i = 0; i < cvEx.getMessages().length; i++) {                    
                    line = cvEx.getMessages()[i];                                        
                    
                    if (line.indexOf("for key") > 0) {                                                                
                        pos = line.indexOf("for key ")+8; 
                        keyNo = Integer.valueOf(line.substring(pos, line.length()));    
                        break;
                    }                    
                }
            }
        }
        return keyNo;
    }
}
